Skip to content

Use temporary variable to initialise array cells #4523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

smowton
Copy link
Contributor

@smowton smowton commented Apr 12, 2019

Previously when dealing with multidimensional arrays we could end up generating expressions
such as array[idx] = ALLOCATE Something; array[idx]->field1 = x; array[idx]->field2 = y;
This was a struggle for symex, as every reference to array[idx] could be a bad dereference,
and so symex::invalid_object (or perhaps some_symbol$object) references were created to account
for this possiblity. By using a temporary (generating code like
t = ALLOCATE Something; t->field1 = x; t->field2 = y; array[idx] = t;) it is much clearer
that the pointer dereferences must refer to the newly allocated object, and so the problematic
case is avoided.

@smowton smowton force-pushed the smowton/feature/use-temporary-to-init-multidimensional-array branch from aeb4a5f to 88a05eb Compare April 12, 2019 12:25
Copy link
Contributor

@romainbrenguier romainbrenguier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we have a test?

Copy link
Contributor

@thk123 thk123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we test this somehow?

else
{
init_expr = allocate_objects.allocate_automatic_local_object(
arraycellref.type(), "new_array_item");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the same name as in the comment above (either this to new_item, or the comment to new_array_item

// alias, cf. the harder-to-analyse
// `some_expr[idx]->x = y; some_expr[idx]->z = w;`
exprt init_expr;
if(new_item_is_primitive)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Any reason to not to do the primitives in the same way, just in the interest of keeping this code simpler?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to keep it simple indeed. array[idx] = nondet(int) -> new_array_item = nondet(int); array[idx] = new_array_item; seems like a disservice to the reader.

Copy link
Contributor

@allredj allredj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✔️
Passed Diffblue compatibility checks (cbmc commit: 88a05eb).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/108119473

Previously when dealing with multidimensional arrays we could end up generating expressions
such as array[idx] = ALLOCATE Something; array[idx]->field1 = x; array[idx]->field2 = y;
This was a struggle for symex, as every reference to array[idx] could be a bad dereference,
and so symex::invalid_object (or perhaps some_symbol$object) references were created to account
for this possiblity. By using a temporary (generating code like
`t = ALLOCATE Something; t->field1 = x; t->field2 = y; array[idx] = t;`) it is much clearer
that the pointer dereferences must refer to the newly allocated object, and so the problematic
case is avoided.
@smowton smowton force-pushed the smowton/feature/use-temporary-to-init-multidimensional-array branch from 88a05eb to 703e7c1 Compare April 12, 2019 13:23
@smowton
Copy link
Contributor Author

smowton commented Apr 12, 2019

Oops, I wrote a test then forgot to commit it, added now.

Copy link
Contributor

@allredj allredj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✔️
Passed Diffblue compatibility checks (cbmc commit: 703e7c1).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/108129000

@smowton smowton merged commit cb25f82 into diffblue:develop Apr 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants